﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Application.SystemManager
{
    public class InventoryUerApp
    {
        private InventoryUerRepository _inventoryUerRepository;
        private OrderRepository _orderRepository;
        private UserRepository _userRepository;

        public InventoryUerApp()
        {
            _inventoryUerRepository = new InventoryUerRepository();
            _orderRepository = new OrderRepository();
            _userRepository = new UserRepository();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="inu"></param>
        /// <returns></returns>
        public int Add(InventoryUer inu)
        {
            return _inventoryUerRepository.Insert(inu);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="inu"></param>
        /// <returns></returns>
        public int Update(InventoryUer inu)
        {
            return _inventoryUerRepository.Update(inu);
        }

        /// <summary>
        /// 根据订单id&产品id获取库存信息
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        public InventoryUer GetInfoByOrder(Guid orderID, Guid productID)
        {
            return _inventoryUerRepository.FindEntity(a => a.OrderID == orderID && a.ProductID == productID);
        }

        /// <summary>
        /// 获取订单下所有产品库存
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public IEnumerable<InventoryUer> GetListByOrder(Guid orderID)
        {
            return _inventoryUerRepository.IQueryable(a => a.OrderID == orderID);
        }

        /// <summary>
        /// 根据产品id获取订单库存列表
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public IEnumerable<InventoryUserView> GetListByProduct(Guid pID)
        {
            //return _inventoryUerRepository.GetListByProduct(pID).OrderByDescending(a => a.Amount);
            return _inventoryUerRepository.GetListByProduct(pID).OrderByDescending(a => a.Amount);
        }

        /// <summary>
        /// 根据订购人获取产品库存
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public IEnumerable<InventoryUer> GetListByUser(Guid userID)
        {
            return _inventoryUerRepository.IQueryable(a => a.UserID == userID);
        }

        /// <summary>
        /// 获取订单下的产品库存
        /// </summary>
        /// <param name="oID"></param>
        /// <param name="proID"></param>
        /// <returns></returns>
        public InventoryUer GetInventoryByOrder(Guid oID, Guid proID)
        {
            return _inventoryUerRepository.FindEntity(a => a.OrderID == oID && a.ProductID == proID);
        }

        /// <summary>
        /// 获取订单库存详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public InventoryUserView GetInventoryUserByID(Guid id)
        {
            return _inventoryUerRepository.GetInventoryUserByID(id);
        }

        /// <summary>
        /// 根据orderID&productID获取订单库存
        /// </summary>
        /// <param name="orderID"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        public InventoryUer GetSingleByOrderAndProduct(Guid orderID, Guid productID)
        {
            return _inventoryUerRepository.FindEntity(a => a.OrderID == orderID && a.ProductID == productID);
        }

        /// <summary>
        /// 根据产品id,获取有库存的工单列表
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public IEnumerable<OrderInfo> GetOrderListByProduct(Guid productID)
        {
            var invList = _inventoryUerRepository.GetListByProduct(productID);
            var query = from inv in _inventoryUerRepository.IQueryable().Where(a => a.ProductID == productID)
                        join ord in _orderRepository.IQueryable() on inv.OrderID equals ord.Id
                        join saler in _userRepository.IQueryable() on inv.UserID equals saler.Id
                        select new OrderInfo
                        {
                            Id = ord.Id,
                            Number = ord.Number,
                            UserId = ord.UserId,
                            CustomeId = ord.CustomeId,
                            SalerName = saler.RealName
                        };
            return query;
        }
    }
}
